﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NextCMS.Models;

public partial class WebAdmin_ManageSites : WebadminBasePage
{
    int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["Id"]), out Id);
        if (!IsPostBack)
        {
            string command = Convert.ToString(Request.QueryString["Type"]);
            ExecuteCommand(command);
            BindSites();
        }
    }
    private void ExecuteCommand(string command)
    {
        if (!String.IsNullOrEmpty(command) && command == "CurrentSite")
        {
            Session["SiteId"] = Convert.ToString(Request.QueryString["Id"]);
            Response.Redirect("ManageSites.aspx");
        }
        else if (!String.IsNullOrEmpty(command) && command == "DefaultSite")
        {
            SiteSettingsHelper.SetSetting("DefaultSite", Convert.ToString(Request.QueryString["Id"]), 0);
            Response.Redirect("ManageSites.aspx");
        }
        else if (!String.IsNullOrEmpty(command) && command == "Delete")
        {
            using (SitesHelper sitesHelper = new SitesHelper())
            {
                using (SiteResourcesHelper siteResourcesHelper = new SiteResourcesHelper())
                {
                    foreach(SiteResourceModel srm in siteResourcesHelper.GetAllBySiteId(Id).ToList())
                    {
                        siteResourcesHelper.Delete(srm.Id);
                    }
                }
                using (SiteLanguagesHelper siteLanguagesHelper = new SiteLanguagesHelper())
                {
                    foreach (SiteLanguageModel slm in ApplicationCache.SiteLanguages.Where(c=>c.SiteId==Id))
                    {
                        siteLanguagesHelper.Delete(slm.Id);
                    }
                }
                using (SiteSettingsHelper siteSettingsHelper = new SiteSettingsHelper())
                {
                    foreach (SiteSettingsModel ssm in ApplicationCache.SiteSettings.Where(c=>c.SiteId==Id).ToList())
                    {
                        siteSettingsHelper.Delete(ssm.Id);
                    }
                }
                using (TemplatesHelper templatesHelper = new TemplatesHelper())
                {
                    foreach (TemplateModel tm in templatesHelper.GetAllBySiteId(Id).ToList())
                    {
                        DeleteTemplate(tm.Id);
                    }
                }
                sitesHelper.Delete(Id);
            }
        }
        
    }
    private void DeleteTemplate(int TemplateId)
    {
        PagesHelper cc = new PagesHelper();
        TemplatesHelper mpc = new TemplatesHelper();

        IEnumerable<PageModel> lstPageModel = cc.GetByMasterPageId(TemplateId).ToList();


        foreach (PageModel pm in lstPageModel)
        {
            PageModulesHelper pmc = new PageModulesHelper();
            IEnumerable<PageModuleModel> lstPagModuleeModel = pmc.GetByPageId(pm.Id).ToList();
            foreach (PageModuleModel pmm in lstPagModuleeModel)
                pmc.Delete(pmm.Id);
            cc.Delete(pm.Id);
        }
        mpc.Delete(Id);
    }
    public string IsSelectedSite(string Id)
    {
        if (Convert.ToString(SiteId) == Id)
            return "../App_Themes/WebAdmin/buttons/radio.png";
        else
            return "../App_Themes/WebAdmin/buttons/radiobutton_no.png";
    }
    public string IsDefaultSite(string Id)
    {
        if (SiteSettingsHelper.GetSetting("DefaultSite", 0) == Id)
            return "../App_Themes/WebAdmin/buttons/radio.png";
        else
            return "../App_Themes/WebAdmin/buttons/radiobutton_no.png";
    }
    private void BindSites()
    {
        SitesHelper pc = new SitesHelper();
        List<SiteModel> lstSiteModel = pc.GetAll().ToList();

        GVSites.DataSource = lstSiteModel;
        GVSites.DataBind();
    }
}